package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.ReceiveAddress;
import util.DBConnection;

public class ReceiveAddressDAOImp implements ReceiveAddressDAO {
	private static final String INSERT = "insert into d_receive_address(id,user_id,receive_name,full_address,postal_code,mobile,phone) "
			+ "values(?,?,?,?,?,?,?)";
	private static final String FINDBYUSERID = "select * from d_receive_address where user_id=?";
	private static final String FINDBYID = "select * from d_receive_address where id=?";
	private static final String UPDATE = "update d_receive_address set user_id=?,receice_name=?,full_address=?,postal_code=?,mobile=?,phone=? where id=?";

	@Override
	public void save(ReceiveAddress r) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		ResultSet rst = null;
		try {
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(INSERT, Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1, r.getId());
			prep.setInt(2, r.getUserId());
			prep.setString(3, r.getReceiveName());
			prep.setString(4, r.getFullAddress());
			prep.setString(5, r.getPostalCode());
			prep.setString(6, r.getMoblie());
			prep.setString(7, r.getPhone());
			prep.executeUpdate();
			rst = prep.getGeneratedKeys();
			rst.next();
		} finally {
			DBConnection.close(rst, prep, conn);
		}
	}

	@Override
	public List<ReceiveAddress> finByUserId(int user_id) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		ResultSet rst = null;
		ReceiveAddress r = null;
		List<ReceiveAddress> addrs = new ArrayList<ReceiveAddress>();
		try {
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(FINDBYUSERID);
			prep.setInt(1, user_id);
			rst = prep.executeQuery();
			while (rst.next()) {
				r = new ReceiveAddress();
				r.setId(rst.getInt("id"));
				r.setUserId(rst.getInt("user_id"));
				r.setReceiveName(rst.getString("receive_name"));
				r.setPostalCode(rst.getString("postal_code"));
				r.setMoblie(rst.getString("mobile"));
				r.setPhone(rst.getString("phone"));
				r.setFullAddress(rst.getString("full_address"));
				addrs.add(r);
			}
		} finally {
			DBConnection.close(rst, prep, conn);
		}
		return addrs;
	}

	@Override
	public ReceiveAddress finById(int id) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		ResultSet rst = null;
		ReceiveAddress r = null;
		try {
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(FINDBYID);
			prep.setInt(1, id);
			rst = prep.executeQuery();
			while (rst.next()) {
				r = new ReceiveAddress();
				r.setId(rst.getInt("id"));
				r.setUserId(rst.getInt("user_id"));
				r.setReceiveName(rst.getString("receive_name"));
				r.setPostalCode(rst.getString("postal_code"));
				r.setMoblie(rst.getString("mobile"));
				r.setPhone(rst.getString("phone"));
				r.setFullAddress(rst.getString("full_address"));
			}
		} finally {
			DBConnection.close(rst, prep, conn);
		}
		return r;
	}

	@Override
	public void update(ReceiveAddress r) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		try {
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(UPDATE);
			prep.setInt(7, r.getId());
			prep.setInt(1, r.getUserId());
			prep.setString(2, r.getReceiveName());
			prep.setString(3, r.getFullAddress());
			prep.setString(4, r.getPostalCode());
			prep.setString(5, r.getMoblie());
			prep.setString(6, r.getPhone());
			prep.executeUpdate();
		} finally {
			DBConnection.close(prep, conn);
		}
	}
}
